package 消息队列.rabbitmq;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DelayExchangeConfig {
    public static final String  EXCHANGE_NAME="chargingDelayExchange";
    public static final String  QUEUE_NAME="chargingDelayQueue";
    //route路由 gateway
    public static final String  ROUTE_KEY="delay.#";
    //配置交换机
    @Bean
    public CustomExchange getExchange(){
        Map<String, Object> hashMap=new HashMap();
        hashMap.put("x-delayed-type","topic");
        //public CustomExchange(String name, String type, boolean durable, boolean autoDelete, Map<String, Object> arguments)
        CustomExchange customExchange = new CustomExchange(EXCHANGE_NAME, "x-delayed-message", false, true, hashMap);
        return  customExchange;
    }
    //配置队列
    @Bean
    public Queue getQueue(){
        Queue queue = new Queue(QUEUE_NAME, false);
        return  queue;
    }
    //将交换机和队列绑定
    @Bean
    public Binding getBinding(@Autowired Queue queue, @Autowired CustomExchange customExchange){
        Binding binding = BindingBuilder.bind(queue).to(customExchange).with(ROUTE_KEY).noargs();
        return binding;
    }
}
